package com.jsb.controller;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.service.H5CourseService;
import com.jsb.common.utils.IdGen;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.H5CourseCust;
import com.jsb.dto.H5CourseDto;
import com.jsb.model.H5Course;
import com.jsb.model.H5CourseExample;
import com.jsb.model.H5CourseExample.Criteria;

import freemarker.template.utility.StringUtil;

@Controller
public class H5CourseController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/h5Course/list.do";
	
	@Resource
	private H5CourseService h5CourseService;
	
	@RequestMapping("/h5Course/list")
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		H5CourseExample h5CourseExample = new H5CourseExample();
		Criteria criteria = h5CourseExample.createCriteria();
		h5CourseExample.setOrderByClause("createDate DESC");
		int totalCount = h5CourseService.countH5Course(h5CourseExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		h5CourseExample.setPageIndex((pageNum - 1) * pageSize);
		h5CourseExample.setPageSize(pageSize);
		List<H5Course> h5Courses = h5CourseService.getPageH5Course(h5CourseExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		for (H5Course h5Course : h5Courses) {
			h5Course.setDes(sublength(h5Course.getDes()));
			h5Course.setCollegeInfo(sublength(h5Course.getCollegeInfo()));
			h5Course.setTrainBack(sublength(h5Course.getTrainBack()));
			h5Course.setLearnObj(sublength(h5Course.getLearnObj()));
			h5Course.setTrainFeature(sublength(h5Course.getTrainFeature()));
			h5Course.setCourseFeature(sublength(h5Course.getCourseFeature()));
			h5Course.setCourseContent(sublength(h5Course.getCourseContent()));
			h5Course.setPresenterInfo(sublength(h5Course.getPresenterInfo()));
			h5Course.setFavorPlan(sublength(h5Course.getFavorPlan()));
			
		}
		model.addAttribute("h5Courses", h5Courses);
		return "/H5Course/list";
	}
	
	@RequestMapping("/h5Course/toAdd")
	public String toAdd(Model model) {
		return "/H5Course/add";
	}

	@RequestMapping("/h5Course/add")
	public String add(Model model, @ModelAttribute("h5Course") H5CourseDto h5CourseCust) throws ParseException {
		H5Course h5Course = new H5Course();
		h5Course.setId(IdGen.uuid());
		h5Course.setCreateDate(new Date());
		try {
			BeanUtils.copyProperties(h5Course, h5CourseCust);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		h5CourseService.addH5Course(h5Course);
		return redirect;
	}

	@RequestMapping("/h5Course/del")
	public String del(Model model,
			@RequestParam(value = "id", required = true) String id) {
		h5CourseService.delH5CourseById(id);
		return redirect;
	}

	@RequestMapping("/h5Course/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "id", required = true) String id) {
		H5Course h5Course = h5CourseService.getH5CourseById(id);
		model.addAttribute("h5Course", h5Course);
		return "/H5Course/edit";
	}

	@RequestMapping("/h5Course/edit")
	public String edit(Model model, @ModelAttribute("h5Course") H5CourseDto h5CourseDto) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		H5Course h5Course = new H5Course();
		PropertyUtils.copyProperties(h5Course, h5CourseDto);
		h5Course.setCreateDate(new Date());
		h5CourseService.updateH5Course(h5Course);
		return redirect;
	}

	@RequestMapping("/h5Course/view")
	public String view(Model model,
			@RequestParam(value = "id", required = true) String id) {
		H5Course h5Course = h5CourseService.getH5CourseById(id);
		model.addAttribute("h5Course", h5Course);
		return "/H5Course/view";
	}
	private String sublength(String s){
		if("".equals(s) || s==null){
			return "";
		}
		int length = s.length();
		if(length <=10){
			return s;
		}else{
			return s.substring(0, 10)+"...";
		}
		
	}
}
